<?php
/*----------------------------------------------------------------------
 * 版权系统管理
 * Author: ming.nie
----------------------------------------------------------------------*/

namespace Common\Logic;

class CopyrightLogic{
	
	/**
	 * 版权系统：同步版权商的影片到版权影片库
	 * @param  array
	 * @return array
	 */
	public function copyrightSyncVideo()
	{
		$model = M();
		
		//查询版权系统有哪些影片
		$sql = 'SELECT video_id FROM mt_copyright_video';
		$rs = $model->query($sql);
		$std_video_ids = array();
		if($rs) {
			foreach($rs as $key => $value) {
				$std_video_ids[] = $value['video_id'];
			}
		}
		$std_video_ids = implode(',', $std_video_ids);
		echo $std_video_ids;
		
		//查询需要同步的影片数据
		$select_sql = '';
		if($std_video_ids) {
			$std_video_ids = ' AND id NOT IN ('.$std_video_ids.')';
		}
		$sql = 'SELECT id,file_name,film_source FROM mt_video WHERE 1 '.$std_video_ids;
		$copyright_video_rs = $model->query($sql);
		
		//查询当前版权商信息
		$video_film_source_options = M('Config')->where('k = "video_film_source"')->find();
		$video_film_source_options = json_decode($video_film_source_options['v'], true);
		
		$copyrightVideoModel = M('CopyrightVideo');
		foreach($copyright_video_rs as $key => $value) {
			$data['video_id'] = $value['id'];
			$data['video_name'] = $value['file_name'];
			$data['copyright_id'] = $value['film_source'];
			$data['copyright_name'] = $video_film_source_options[$value['film_source']];
			$copyrightVideoModel->data($data)->add();
		}
	}
	/**
	 * [getReportMessages 获取用户报错信息, 用于同步到转码中心]
	 * @param  [type] $params [description]
	 * @return [type]         [description]
	 */
	public function getReportMessages($params) {

		$data_ctime = isset( $params['data_ctime'] ) ? intval( $params['data_ctime'] ) : '';
        $res_data = array();

        if( $data_ctime === '' ) {
            return array();
        }

        $res_data = M('messages')->where(array('ctime' => array('gt', $data_ctime)))->order('ctime ASC')->limit(100)->select();
        $callback_data = array();
        $callback_data['code'] = 0;
        $callback_data['data'] = $res_data;

        return $callback_data;
	}
}
